<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-03-19
 * Time: 15:02
 */

class User extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 管理元列表
     */
    public function admin_list(){
        $pageTitle = "管理列表";
        $adminList = $this->UserAdmin->getAdminList();

        $this->load->view('user/admin_list',array(
            "pageTitle" => $pageTitle,
            "adminList" => $adminList
        ));
    }
    /**
     * 删除管理员
     */
    public function admin_del(){
        $uid = $this->input->get_post("uid");
        $uids = explode(",", $uid);
        foreach($uids as $uid){
            $this->UserAdmin->del_data(array("uid" => $uid, "type" => 0));
        }
        $this->AdminOpenationModel->insertLog($uid,9,'删除管理员 uid：'.$uid);
        std_out(0, "");
    }

    /**
     * 开启 / 取消 火眼金星
     */
    public function set_persp(){
        $uid = $this->input->get_post("uid");
        $is_persp = $this->input->get_post("is_persp") ? $this->input->get_post("is_persp") : 0;

        $param = array("is_persp" => $is_persp);
        $where = array("uid" => $uid);
        $this->UserAdmin->update_data($param, $where);
        $this->AdminOpenationModel->insertLog($uid,10,'开启火眼金星 uid：'.$uid);
        std_out(0, "");
    }
    /**
     * 添加管理员
     */
    public function admin_edit(){
        $pageTitle = "添加管理";
        if($_REQUEST["submits"]){
            $uid = $this->input->get_post('uid');
            $password = $this->input->get_post('password');
            if(empty($uid)){
                std_out(-1, "非法请求");
            }
            $userAdmin  = $this->UserAdmin->query_one(array("uid" => $uid));
            if(!empty($userAdmin)){
                std_out(-1, "用户已存在");
            }
            $userInfo = $this->UserModel->query_one(array("uid" => $uid));
            if(empty($userInfo)){
                std_out(-1, "用户不存在");
            }
            $param = array("uid" => $uid, "type" => 0, "add_time" => time());
            $this->UserAdmin->insert_one($param);
            $this->UserModel->update_data(array('password'=>$password),array('uid'=>$uid));
            $this->AdminOpenationModel->insertLog($uid,11,'添加管理员 uid：'.$uid);
            std_out(0, "");
        }
        $this->load->view('user/admin_edit', array(
            'pageTitle' => $pageTitle,
        ));
    }

    /**
     * 用户列表
     */
    public function user_list(){
        $pageTitle = "用户列表";
        $user_type = $this->input->get_post("user_type") ? $this->input->get_post("user_type") : 0;
        $user_status = $this->input->get_post("user_status") ? $this->input->get_post("user_status") : 0;
        $mobile = $this->input->get_post("mobile") ? $this->input->get_post("mobile") : "";
        $user_source = $this->input->get_post("user_source") ? $this->input->get_post("user_source") : "";
        $page = $this->input->get_post("page") ? $this->input->get_post("page") : 1;
        $pagesize = $this->input->get_post("pagesize") ? $this->input->get_post("pagesize") : 20;
        $order_by = $this->input->get_post("order_by") ? $this->input->get_post("order_by") : "regdate";
        $where = array();
        $arg = "&order_by=".$order_by;
        if(!empty($user_type)){
            $where["user_type"] = $user_type;
            $arg.= "&user_type=".$user_type;
        }
        if(!empty($user_status)){
            $where["user_status"] = $user_status;
            $arg.= "&user_status=".$user_status;
        }
        if(!empty($mobile)){
            $where["mobile"] = $mobile;
            $arg.= "&mobile=".$mobile;
        }
        if(!empty($user_source)){
            $where["user_source"] = $user_source;
            $arg.= "&user_source=".$user_source;
        }
        $orderBy = array($order_by => "desc");
        $userList = $this->UserModel->getList($where, $page, $pagesize, $orderBy);
        $num = $this->UserModel->getListNum($where);

        $sourceList = $this->UserModel->distinctSource();

        $url = "index.php?c=user&m=user_list".$arg;
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('user/user_list',array(
            "pageTitle" => $pageTitle,
            "userList" => $userList,
            "multipage" => $multipage,
            "sourceList" => $sourceList
        ));
    }

    /**
     * 用户数据统计，详情页面
     */
    public function user_info(){

        $pageTitle = '用户个人资料';
        $uid = $this->input->get_post('uid');
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        $pagesize = $this->input->get_post('pagesize') ? $this->input->get_post('pagesize') : 20;
        $op = $this->input->get_post('op') ? $this->input->get_post('op') : 'user_info';
        $userInfo = $this->UserModel->getUserInfo($uid);
        $url = "index.php?c=user&m=user_info&op=".$op."&page=".$page.'&pagesize='.$pagesize.'&uid='.$uid;
        if($op == 'reply'){
            $list=$this->CommentModel->getList(array('uid'=>$uid),$page,$pagesize,array('add_time'));
            $num=$this->CommentModel->getListNum(array('uid'=>$uid));
            $multipage = multipage($num, $page, $pagesize, $url);
        }
        if($op == 'post'){
            $list=$this->PostModel->getList(array('uid'=>$uid),$page,$pagesize,array('add_time'=>'desc'));
            $num=$this->PostModel->getListNum(array('uid'=>$uid));
            $multipage = multipage($num, $page, $pagesize, $url);
        }
        if($op == 'login'){
            $list=$this->CreditUserLogModel->getList(array('uid'=>$uid,'action_code'=>array('day_login','first_login')),$page,$pagesize,array('add_time'));
            $num=$this->CreditUserLogModel->getListNum(array('uid'=>$uid,'action_code'=>array('day_login','first_login')));
            $multipage = multipage($num, $page, $pagesize, $url);
        }
        $task=array();
        if($op == 'credit'){
            //查询任务名
            $task=$this->TaskModel->get_code_title();
            $list=$this->CreditUserLogModel->getList(array('uid'=>$uid),$page,$pagesize,array('add_time'=>'desc'));
            $num=$this->CreditUserLogModel->getListNum(array('uid'=>$uid));
            $multipage = multipage($num, $page, $pagesize, $url);
        }

        $this->load->view('user/user_info',array(
            'pageTitle'=>$pageTitle,
            'uid'=>$uid,
            'multipage'=>$multipage,
            'task'=>$task,
            'list'=>$list,
            'userInfo'=>$userInfo
        ));
    }

    /**
     * 编辑用户信息
     */
    public function user_edit(){
        $pageTitle = '编辑用户信息';
        $uid = $this->input->get_post('uid');

        if($_REQUEST["submits"]){
            $nickname = $this->input->get_post('nickname');
            $regdate = $this->input->get_post('regdate');
            $regdate = !empty($regdate) ? strtotime($regdate) : 0;
            $birth = $this->input->get_post('birth');
            $sign = $this->input->get_post('sign');
            $avator_id = $this->input->get_post('avator_id');
            $birth = !empty($birth) ? strtotime($birth) : 0;
            $sex = $this->input->get_post('sex') ? $this->input->get_post('sex') : 0;

            $param = array("nickname"=>$nickname, "regdate" => $regdate, "birth" => $birth, "avator_id" => $avator_id,"sex" => $sex, "sign" => $sign);
            $this->UserModel->update_data($param, array("uid" => $uid));
            $this->AdminOpenationModel->insertLog($uid,12,'编辑用户信息 uid：'.$uid);
            std_out(0, "");
        }
        $user = $this->UserModel->getUserInfo($uid);

        $this->load->view('user/user_edit',array(
            'pageTitle'=>$pageTitle,
            'uid' => $uid,
            'user' => $user
        ));
    }

    /**
     * 编辑用户
     */
    public function user_update(){
        $pageTitle = '编辑用户信息';
        $uid = $this->input->get_post('uid');

        if($_REQUEST["submits"]){
            $mobile = $this->input->get_post('mobile');
            $nickname = $this->input->get_post('nickname');
            $regdate = $this->input->get_post('regdate');
            $regdate = !empty($regdate) ? strtotime($regdate) : 0;
            $birth = $this->input->get_post('birth');
            $sign = $this->input->get_post('sign');
            $avator_id = $this->input->get_post('avator_id');
            $birth = !empty($birth) ? strtotime($birth) : 0;
            $sex = $this->input->get_post('sex') ? $this->input->get_post('sex') : 0;
            if(empty($nickname)){
                std_out(-1, "手机号格式不正确或者昵称为空");
            }
            $userInfo = $this->UserModel->query_one(array("mobile" => $mobile));
            if(!empty($userInfo) && $userInfo["uid"] != $uid){
                std_out(-1, "手机号已存在，请换个手机号");
            }
            $param = array("mobile" => $mobile, "nickname"=>$nickname, "regdate" => $regdate, "birth" => $birth, "avator_id" => $avator_id,"sex" => $sex, "sign" => $sign);
            $this->UserModel->update_data($param, array("uid" => $uid));
            $this->AdminOpenationModel->insertLog($uid,12,'编辑用户信息 uid：'.$uid);

            std_out(0, "");
        }
        $user = $this->UserModel->getUserInfo($uid);

        $this->load->view('user/user_update',array(
            'pageTitle'=>$pageTitle,
            'uid' => $uid,
            'user' => $user
        ));
    }

    /**
     * 白名单列表
     */
    public function write_list(){
        $pageTitle = "用户列表";
        $mobile = $this->input->get_post("mobile") ? $this->input->get_post("mobile") : "";
        $page = $this->input->get_post("page") ? $this->input->get_post("page") : 1;
        $pagesize = $this->input->get_post("pagesize") ? $this->input->get_post("pagesize") : 20;
        $where = array();
        if(!empty($mobile)){ $where["mobile"] = $mobile; }
        $userList = $this->UserWriteList->getWriteList($where, $page, $pagesize);
        $num = $this->UserWriteList->getWriteListNum($where);

        $url = "index.php?c=user&m=write_list";
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('user/write_list',array(
            "pageTitle" => $pageTitle,
            "userList" => $userList,
            "multipage" => $multipage
        ));
    }

    /**
     * 添加白名单
     */
    public function add_write(){
        $pageTitle = "添加白名单";
        if($_REQUEST["submits"]){
            $mobile = $this->input->get_post("mobile");
            $userWrite = $this->UserWriteList->query_one(array("mobile" => $mobile));
            if(!empty($userWrite)){
                std_out(-1, "该手机号已添加");
            }
            $loginUser = getLoginUser();
            $uid = $loginUser["uid"];
            $param = array("mobile" => $mobile, "add_uid" => $uid, "add_time" => time());
            $this->UserWriteList->insert_one($param);
            $this->AdminOpenationModel->insertLog($uid,13,'添加白名单 uid：'.$uid);
            std_out(0, "");
        }
        $this->load->view('user/add_write', array(
            'pageTitle' => $pageTitle,
        ));
    }
    /**
     * 移除白名单
     */
    public function del_write(){
        $id = $this->input->get_post("id") ? $this->input->get_post("id") : "";
        if(empty($id)){
            std_out(-1, "ID为空");
        }
        $this->UserWriteList->del_data(array("id"=>$id));
        $this->AdminOpenationModel->insertLog($id,14,'移除白名单 uid：'.$id);
        std_out(0, "");
    }

    /**
     * 开启 / 取消 前端审核
     */
    public function set_audit(){
        $uid = $this->input->get_post("uid");
        $is_audit = $this->input->get_post("is_audit") ? $this->input->get_post("is_audit") : 0;

        $param = array("is_audit" => $is_audit);
        $where = array("uid" => $uid);
        $this->UserAdmin->update_data($param, $where);
        $this->AdminOpenationModel->insertLog($uid,15,'开启 / 取消 前端审核 uid：'.$uid);
        std_out(0, "");
    }

    /**
     * 管理员操作记录
     * name: admin_log
     * @throws Exception
     * @Author:taiyang
     */
    public function admin_log(){
        //
        $type_arr = $this->AdminOpenationModel->type;
        $uid = $this->input->get_post("uid")?$this->input->get_post("uid"):'';
        $s_type = $this->input->get_post("s_type")?$this->input->get_post("s_type"):'';
        $post_id = $this->input->get_post("post_id")?$this->input->get_post("post_id"):'';
        $type = $this->input->get_post("type")?$this->input->get_post("type"):'';//type=1帖子数据(1,2,3,6,8)
        $page=$this->input->get_post('page')?$this->input->get_post('page'):1;
        $pagesize=$this->input->get_post('pagesize')?$this->input->get_post('pagesize'):10;
        //获取管理列表
        $admin_list=$this->UserAdmin->getAdminList();
        //获取操作列表
        $list=$this->AdminOpenationModel->getLogList($uid,$type,$s_type,$post_id,$page,$pagesize);
        $num=$this->AdminOpenationModel->getLogNum($uid,$type,$s_type,$post_id);
        $url = 'index.php?c=user&m=admin_log&uid='.$uid.'&type='.$type.'&s_type='.$s_type.'&post_id='.$post_id;
        $multipage = multipage($num, $page, $pagesize, $url);
        $pageTitle = "操作记录";
        $this->load->view('user/admin_log',array(
            "pageTitle" => $pageTitle,
            "s_type" => $s_type,
            "post_id" => $post_id,
            "type_arr" => $type_arr,
            "type" => $type,
            "uid" => $uid,
            "list" => $list,
            "admin_list" => $admin_list,
            "multipage" => $multipage,
        ));
    }

    /**
     * 禁言 / 封号 用户
     */
    public function user_jin(){
        $user_ids = $this->input->get_post("user_ids");
        $type = $this->input->get_post("type");
        $report = $this->input->get_post("report")?$this->input->get_post("report"):"";
        $time_range = $this->input->get_post("time_range");
        if(empty($user_ids) || empty($type) || empty($time_range)){
            showMessage("index.php?c=user&m=user_list", -1, "非法请求");
        }
        $loginUser = getLoginUser();
        $uids = explode(",", $user_ids);
        foreach($uids as $uid){
            $this->UserModel->user_jin($type,$time_range,$uid,$loginUser);
        }
        showMessage("index.php?c=user&m=user_list");
    }

    /**
     * 发帖需要审核的用户列表
     */
    public function post_review(){
        $pageTitle = "用户列表";
        $page = $this->input->get_post("page") ? $this->input->get_post("page") : 1;
        $pagesize = $this->input->get_post("pagesize") ? $this->input->get_post("pagesize") : 20;
        $where = array("post_review" => 1);
        $orderBy = array("last_login" => "desc");
        $userList = $this->UserModel->getList($where, $page, $pagesize, $orderBy);
        $num = $this->UserModel->getListNum($where);

        $url = "index.php?c=user&m=user_list";
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('user/post_review',array(
            "pageTitle" => $pageTitle,
            "userList" => $userList,
            "multipage" => $multipage
        ));
    }

    /**
     * 添加需要审核的用户
     */
    public function add_review(){
        $pageTitle = "添加管理";
        if($_REQUEST["submits"]){
            $mobile = $this->input->get_post('mobile');
            if(empty($mobile)){
                std_out(-1, "非法请求");
            }
            $user = $this->UserModel->query_one(array("mobile" => $mobile));
            if(empty($user)){
                std_out(-1, "用户手机号不存在");
            }
            $userCount = $this->UserCount->query_one(array("uid" => $user["uid"]));
            if($userCount["is_post_review"] == 1){
                std_out(-1, "已经添加过了");
            }
            $param = array("is_post_review" => 1);
            $this->UserCount->update_data($param, array("uid" => $user["uid"]));

            std_out(0, "");
        }
        $this->load->view('user/add_review', array(
            'pageTitle' => $pageTitle,
        ));
    }

    /**
     * 移除需要审核的用户
     */
    public function del_review(){
        $uid = $this->input->get_post('uid');
        if(empty($uid)){
            std_out(-1, "非法请求");
        }
        $param = array("is_post_review" => 0);
        $this->UserCount->update_data($param, array("uid" => $uid));

        std_out(0, "");
    }

    /**
     * 给用户加V
     */
    public function jia_v(){
        $uid = $this->input->get_post('uid');
        $is_v = $this->input->get_post('is_v');

        $param = array("is_v" => $is_v);
        $this->UserModel->update_data($param, array("uid" => $uid));

        std_out(0, "");
    }
}